/**
    Library containing the definitions of functionalities for the implementation of a "software"
    KP by means of multihreading.

    @author Tiziano De Matteis

/* TODO (dematteis#1#): Chiaramente va modificato in accordo a quanto si decidera' */

/* TODO (dematteis#1#): Gestion errori? che si fa? */
#include <pthread.h>
#include "blk_queue.h"
#include "channel.h"

#define DQUEUE_SIZE 11 //initial size of the delegation queue

typedef struct{
    pthread_t id; //communicator id
    queue_t *dQueue; //queue that will contains the send delegation
} communicator_t;

/**
    Data structure for a send delegation from the worker to the communicator thread.
    It will contain a reference to the message and to the channel. It has also
    the necessary for the worker thread to be informed of the completion of that send
    (substantially a condition variable and the relative semaphore)
*/
typedef struct{
    struct channel_t *ch; //pointer to the channel
    char *msg; //pointer to the message
    lock_variable_t lV;
    cond_variable_t ticket;
}delegation_t;

/**
    This function will create a communicator thread that will be scheduled on the same
    core where the caller resides.

    @param core_mapping: an integer vector whose size is equal to the number of logic processor
        present in the machine. core_mapping[i], where i is the processor ID of the worker thread,
        indicates the ID of the logical processor where the communicator thread must be scheduled in
        order to be on the same core of the partner
    @return a communicator_t structure containing the necessary infos or NULL in case of error
*/


communicator_t * initCommunicator(int core_mapping[]);


/**
    This function will stop the communicator and will wait for its termination.
    This will happen only when all the delegated send are completed.
*/
void finalizeCommunicator(communicator_t *kp);

